package com.nkbh.sevenday.mykotlin.activity

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.ActionBarDrawerToggle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.nkbh.sevenday.mykotlin.R
import com.nkbh.sevenday.mykotlin.fragment.ContentFrament
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initData()

    }

    fun initData(){
        val func=ArrayList<String>()
        func.add("用户信息")
        func.add("关于")
        func.add("设置")
        val icon = ArrayList<Int>()
        icon.add(R.mipmap.user)
        icon.add(R.mipmap.about)
        icon.add(R.mipmap.setting)
        val adapter = DrawerAdapter(this, func, icon)
        leftListview.adapter = adapter

        toolbar.title=""
        setSupportActionBar(toolbar)
        //左上角图标可用
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeButtonEnabled(true)
        val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
        drawerToggle.syncState()


        val sfm = supportFragmentManager
        val contentFrament = ContentFrament()

        sfm.beginTransaction().replace(R.id.contentFrame, contentFrament).commit()


    }


    internal inner class DrawerAdapter(var context: Context, var func: List<String>, var icon: List<Int>) : BaseAdapter() {

        override fun getCount(): Int {
            return func.size
        }

        override fun getItem(position: Int): Any {
            return func[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var convertView = convertView
            val viewHolder: ViewHolder
            if (convertView == null) {
                viewHolder = ViewHolder()
                convertView = LayoutInflater.from(context).inflate(R.layout.listview_drawer, parent, false)
                viewHolder.img_icon = convertView!!.findViewById(R.id.img_icon)
                viewHolder.tv_func_name = convertView.findViewById(R.id.tv_func_name)
                convertView.tag = viewHolder
            } else {
                viewHolder = convertView.tag as ViewHolder
            }

            viewHolder.tv_func_name!!.text = func[position]
            viewHolder.img_icon!!.setBackgroundResource(icon[position])

            return convertView
        }


        internal inner class ViewHolder {
            var img_icon: ImageView? = null
            var tv_func_name: TextView? = null
        }

    }

}
